home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / checkbox / component.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  6.2 KB  |  167 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import re
  6. import itertools
  7. import logging
  8. import posixpath
  9. from checkbox.lib.cache import cache
  10. from checkbox.lib.path import path_expand_recursive
  11. from checkbox.properties import List, String
  12. from checkbox.variables import get_variables
  13.  
  14. class ComponentSection(object):
  15.     '''
  16.     Component section which is essentially a container of modules. These
  17.     map to the modules referenced in the configuration passed as argument
  18.     to the constructor.
  19.     '''
  20.     modules = List(type = String())
  21.     whitelist = List(type = String(), default_factory = (lambda : ''))
  22.     blacklist = List(type = String(), default_factory = (lambda : ''))
  23.     
  24.     def __init__(self, config, name):
  25.         '''
  26.         Constructor which takes a configuration instance and name as
  27.         argument. The former is expected to contain modules.
  28.         '''
  29.         self._config = config
  30.         self.name = name
  31.         self.modules = config.modules
  32.         self.whitelist = config.get('whitelist')
  33.         self.blacklist = config.get('blacklist')
  34.  
  35.     
  36.     def get_names(self):
  37.         '''
  38.         Get all the module names contained in the filenames or directories
  39.         for this section.
  40.         '''
  41.         whitelist_patterns = [ re.compile('^%s$' % r) for r in self.whitelist ]
  42.         blacklist_patterns = [ re.compile('^%s$' % r) for r in self.blacklist ]
  43.         names = set()
  44.         filenames = [](*[ path_expand_recursive(m) for m in self.modules ])
  45.         for filename in filenames:
  46.             name = posixpath.basename(filename)
  47.             name = name.replace('.py', '')
  48.             if whitelist_patterns:
  49.                 if not _[4]:
  50.                     logging.info('Not whitelisted module: %s', name)
  51.                     continue
  52.                 
  53.             elif blacklist_patterns:
  54.                 if _[5]:
  55.                     logging.info('Blacklisted module: %s', name)
  56.                     continue
  57.                 
  58.             
  59.             names.add(name)
  60.         
  61.         return list(names)
  62.  
  63.     get_names = cache(get_names)
  64.     
  65.     def has_module(self, name):
  66.         '''
  67.         Check if the given name is in this section.
  68.         '''
  69.         return name in self.get_names()
  70.  
  71.     
  72.     def load_module(self, name):
  73.         '''
  74.         Load a single module by name from this section.
  75.         '''
  76.         logging.info('Loading module %s from section %s', name, self.name)
  77.         if not self.has_module(name):
  78.             raise Exception, 'No such such module: %s' % name
  79.         self.has_module(name)
  80.         filenames = [](*[ path_expand_recursive(m) for m in self.modules ])
  81.         for filename in filenames:
  82.             if filename.endswith('.py') and posixpath.exists(filename):
  83.                 basename = posixpath.basename(filename)
  84.                 basename = basename.replace('.py', '')
  85.                 if basename == name:
  86.                     globals = { }
  87.                     exec open(filename) in globals
  88.                     if 'factory' not in globals:
  89.                         raise Exception, "Variable 'factory' not found in: %s" % filename
  90.                     'factory' not in globals
  91.                     module = globals['factory']()
  92.                     module.__module__ = name
  93.                     config_name = '/'.join([
  94.                         self.name,
  95.                         name])
  96.                     config = self._config.parent.get_section(config_name)
  97.                     variables = get_variables(module)
  98.                     environ = []([ (k.lower(), v) for k, v in os.environ.items() ])
  99.                     for attribute, variable in variables.iteritems():
  100.                         if config and attribute.name in config:
  101.                             value = config.get(attribute.name)
  102.                             variable.set(value)
  103.                             continue
  104.                         []
  105.                         value = variable.get()
  106.                         if isinstance(value, basestring):
  107.                             value = value % environ
  108.                             variable.set(value)
  109.                             continue
  110.                         dict
  111.                         if isinstance(value, list):
  112.                             value = [ v % environ for v in value ]
  113.                             variable.set(value)
  114.                             continue
  115.                         []
  116.                     
  117.                     for attribute, variable in variables.iteritems():
  118.                         value = variable.get()
  119.                         if value is None and variable._required:
  120.                             raise Exception, "Configuration '%s' missing required attribute: %s" % (config_name, attribute.name)
  121.                         variable._required
  122.                     
  123.                     return module
  124.                 continue
  125.             basename == name
  126.         
  127.         raise Exception, "Failed to find module '%s' in: %s" % (name, filenames)
  128.  
  129.     
  130.     def load_modules(self):
  131.         '''
  132.         Load all modules contained in this section.
  133.         '''
  134.         modules = []
  135.         for name in self.get_names():
  136.             module = self.load_module(name)
  137.             modules.append(module)
  138.         
  139.         return modules
  140.  
  141.  
  142.  
  143. class ComponentManager(object):
  144.     '''
  145.     Component manager which is essentially a container of sections.
  146.     '''
  147.     _section_factory = ComponentSection
  148.     
  149.     def __init__(self, config):
  150.         '''
  151.         Constructor which takes a configuration instance as argument. This
  152.         will be used to load sections by name.
  153.         '''
  154.         self._config = config
  155.  
  156.     
  157.     def load_section(self, name):
  158.         '''
  159.         Load a section by name which must correspond to a module in the
  160.         configuration instance pased as argument to the constructor.
  161.         '''
  162.         logging.info('Loading component section %s', name)
  163.         config = self._config.get_section(name)
  164.         return self._section_factory(config, name)
  165.  
  166.  
  167.